#!/usr/bin/node
const { EventEmitter } = require('./05-event.js');

class Dog extends EventEmitter {
    #name;
    #energy;
    #timer;

    constructor(name, energy) {
        super();
        this.#name = name;
        this.#energy = energy;
        this.#timer = setInterval(() => {
            if (this.#energy > 0) {
                this.emit('bark');
                this.#energy--;
            } else {
                clearInterval(this.#timer);
            }
        }, 1000);
    }

    get name() {
        return this.#name;
    }

    get energy() {
        return this.#energy;
    }

    set energy(newEnergy) {
        this.#energy = newEnergy;
        if (this.#energy <= 0) {
            clearInterval(this.#timer);
        } else {
            clearInterval(this.#timer);
            this.#timer = setInterval(() => {
                if (this.#energy > 0) {
                    this.emit('bark');
                    this.#energy--;
                } else {
                    clearInterval(this.#timer);
                }
            }, 1000);
        }
    }
}

module.exports = Dog;